package cn.Mmovie.servlet;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.service.AuditoriumService;
import cn.Mmovie.service.CinemaService;
import cn.Mmovie.service.impl.AuditoriumServiceImpl;
import cn.Mmovie.service.impl.CinemaServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "AdminAuditoriumAddServlet", value = "/admin/auditorium_add")
public class AdminAuditoriumAddServlet extends HttpServlet {
    private AuditoriumService auditoriumService=new AuditoriumServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        Integer cinema= Integer.valueOf(request.getParameter("cinema"));
        Integer capacity=Integer.parseInt(request.getParameter("capacity"));
        Cinema c=new Cinema(cinema);
        Auditorium a=new Auditorium(c,name,capacity);
        Integer res=auditoriumService.addAuditorium(a);
        if (res>0){
            request.setAttribute("msg","放映厅添加成功！");
            request.getRequestDispatcher("/admin/auditorium_list").forward(request,response);
        }else if (res==-1){
            request.setAttribute("failMsg","放映厅已存在，请重试");
            request.setAttribute("cinema",c);
            request.getRequestDispatcher("/admin/add_auditorium").forward(request,response);
        }else {
            request.setAttribute("failMsg","放映厅添加失败！");
            request.setAttribute("cinema",c);
            request.getRequestDispatcher("/admin/add_auditorium").forward(request,response);
        }
    }
}
